﻿/*
 * Packet.h
 */

#ifndef __GDK_PACKET_H__
#define __GDK_PACKET_H__


#include <gdk/core/Types.h>
#include <gdk/core/Exception.h>
#include <gdk/core/Stream.h>
#include <gdk/core/Buffer.h>



namespace gdk
{



	class GDK_CORE_EXPORT PacketWriter
	{
		OutputStream	_os;

	public:
		__INLINE__ bool isNull(void) { return _os.isNull(); }
		__INLINE__ bool attach(Output *o) { _os.attach(o); return true; }
		Output::Handle detach(void);

		int write(InputStream &is, bool throwIfError = false) __THROW1;
		int write(const Buffer &pkg, bool throwIfError = false) __THROW1;

		__INLINE__ PacketWriter& operator <<(InputStream &is) __THROW1 { write(is, true); return *this; }
		__INLINE__ PacketWriter& operator <<(const Buffer &pkg) __THROW1 { write(pkg, true); return *this; }
	};


	class GDK_CORE_EXPORT PacketReader
	{
		InputStream	_is;
		int			_bufPointer;
		uint8_t		_buf[(sizeof(ssize_t) * 8 + 6) / 7];
		ssize_t		_pkgSize;
		IO::Handle	_pkg;

	public:
		__INLINE__ PacketReader(void) : _bufPointer(0) {}
		__INLINE__ bool isNull(void) { return _is.isNull(); }
		__INLINE__ bool attach(Input *i) { _is.attach(i); return true; }
		Input::Handle detach(void);

		int read(InputStream &is, bool throwIfError = false) __THROW1;
		int read(Buffer &pkg, bool throwIfError = false) __THROW1;

		__INLINE__ PacketReader& operator >>(InputStream &is) __THROW1 { read(is, true); return *this; }
		__INLINE__ PacketReader& operator >>(Buffer &pkg) __THROW1 { read(pkg, true); return *this; }
	};



}//namespace gdk


#endif //__GDK_PACKET_H__
